using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.Networking;
using System.Collections.Generic;

public class IncrementalUpdate : MonoBehaviour
{
    private string serverUrl = "https://your-server.com/AssetBundles/";
    private string localVersionPath = Application.persistentDataPath + "/versions.json";

    void Start()
    {
        StartCoroutine(CheckForUpdates());
    }

    IEnumerator CheckForUpdates()
    {
        string serverVersionUrl = serverUrl + "versions.json";
        UnityWebRequest request = UnityWebRequest.Get(serverVersionUrl);
        yield return request.SendWebRequest();

        if (request.result != UnityWebRequest.Result.Success)
        {
            Debug.LogError("Failed to download version file.");
            yield break;
        }

        string serverVersionData = request.downloadHandler.text;
        VersionInfo serverVersionInfo = JsonUtility.FromJson<VersionInfo>(serverVersionData);

        VersionInfo localVersionInfo = LoadLocalVersionInfo();

        foreach (var bundleInfo in serverVersionInfo.bundles)
        {
            if (!localVersionInfo.Contains(bundleInfo) || localVersionInfo.GetVersion(bundleInfo.name) != bundleInfo.version)
            {
                StartCoroutine(DownloadAndUpdateBundle(bundleInfo.name, bundleInfo.version));
            }
        }
    }

    IEnumerator DownloadAndUpdateBundle(string bundleName, string version)
    {
        string bundleUrl = serverUrl + bundleName;
        UnityWebRequest request = UnityWebRequest.Get(bundleUrl);
        yield return request.SendWebRequest();

        if (request.result != UnityWebRequest.Result.Success)
        {
            Debug.LogError("Failed to download bundle: " + bundleName);
            yield break;
        }

        byte[] data = request.downloadHandler.data;
        string localBundlePath = Path.Combine(Application.persistentDataPath, bundleName);
        File.WriteAllBytes(localBundlePath, data);

        UpdateLocalVersionInfo(bundleName, version);
    }

    VersionInfo LoadLocalVersionInfo()
    {
        if (File.Exists(localVersionPath))
        {
            string localVersionData = File.ReadAllText(localVersionPath);
            return JsonUtility.FromJson<VersionInfo>(localVersionData);
        }
        return new VersionInfo();
    }

    void UpdateLocalVersionInfo(string bundleName, string version)
    {
        VersionInfo localVersionInfo = LoadLocalVersionInfo();
        localVersionInfo.Update(bundleName, version);
        File.WriteAllText(localVersionPath, JsonUtility.ToJson(localVersionInfo));
    }
}

[System.Serializable]
public class VersionInfo
{
    public List<BundleInfo> bundles;

    public bool Contains(BundleInfo bundleInfo)
    {
        return bundles.Exists(b => b.name == bundleInfo.name);
    }

    public string GetVersion(string bundleName)
    {
        return bundles.Find(b => b.name == bundleName).version;
    }

    public void Update(string bundleName, string version)
    {
        BundleInfo bundle = bundles.Find(b => b.name == bundleName);
        if (bundle != null)
        {
            bundle.version = version;
        }
        else
        {
            bundles.Add(new BundleInfo { name = bundleName, version = version });
        }
    }
}

[System.Serializable]
public class BundleInfo
{
    public string name;
    public string version;
}